package me.passtime.projt.fee;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.AndroidCharacter;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.unionpay.upomp.bypay.util.UPOMP;
import com.unionpay.upomp.bypay.util.Utils;
import me.passtime.projt.Home;
import me.passtime.projt.R;
import me.passtime.projt.util.*;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-6
 * Time: 上午9:43
 */
public class PaymentConfirmPage extends Activity implements View.OnClickListener {
    private Button mUserInfo;
    private TextView mAmount;
    private TextView mPhoneNumber;
    private double amount = 0;
    private String phoneNumber;
    private String orderId;
    private String payPkg;
    private String cardNo;
    private String cardPwd;
    private boolean isIvrPay = false;

    public static final String MY_PKG = "me.passtime.projt";
    public static final String PLUGIN_PKG_PAYMENT = "com.unionpay.upomp.bypay.paymain";

    private boolean isFirst = false;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 12;

    private IvrController mIvrController;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment_confirm);
        mIvrController=new IvrController(this);

        Bundle extras = getIntent().getExtras();
        amount = extras.getDouble("amount");
        phoneNumber = extras.getString("phoneNumber");
        orderId = extras.getString("orderId");
        payPkg = extras.getString("payPkg");
        isIvrPay = extras.getBoolean("isIvrPay");
        if (isIvrPay) {
            cardNo = extras.getString("cardNo");
            cardPwd = extras.getString("cardPwd");
        }

        mUserInfo = (Button) this.findViewById(R.id.payment_confirm_person_btn);
        mUserInfo.setText(UserHolder.phoneNumber + "\r\n" + UserHolder.userName);
        mAmount = (TextView) this.findViewById(R.id.payment_confirm_amount);
        mAmount.setText(amount + "元");
        mPhoneNumber = (TextView) this.findViewById(R.id.payment_confirm_phone_number);
        mPhoneNumber.setText(phoneNumber);

        this.findViewById(R.id.payment_confirm_confirm_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_confirm_back_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_confirm_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_confirm_my_account_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_confirm_fee_records_btn).setOnClickListener(this);
        this.findViewById(R.id.payment_confirm_exit).setOnClickListener(this);
        isFirst = true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.payment_confirm_back_btn:
                PageJumpUtils.jump(this, Home.class);
                break;
            case R.id.payment_confirm_logout_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.payment_confirm_my_account_btn:
                PageJumpUtils.jumpToManageAccountPage(this);
                break;
            case R.id.payment_confirm_fee_records_btn:
                PageJumpUtils.jumpToQueryPaymentPage(this);
                break;
            case R.id.payment_confirm_exit:
                AlertDialogUtils.showYesNoDiaLog(this, "是否退出系统?", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        SystemUtils.killSelf();
                        finish();
                    }
                });
                break;
            case R.id.payment_confirm_confirm_btn:
                doPay();
                break;
        }

    }

    private void showPaymentSuccessPage() {
        Intent intent = new Intent(this, PaymentSuccessPage.class);
        intent.putExtra("amount", amount);
        intent.putExtra("phoneNumber", phoneNumber);
        this.startActivity(intent);
    }

    private void doPay() {
        this.showDialog(DIALOG_PROGRESS);
        if (isIvrPay) {
            mIvrController.payUserIvrInterface(phoneNumber,cardNo,cardPwd,amount);
        } else {
            startUpompIntent(orderId, payPkg);
        }
    }

    public void startUpompIntent(String orderId, String payPkg) {
        isFirst = false;
        Utils.setPackageName(MY_PKG);
        UPOMP.init();
        // 设置跳转到手机POS Intent
        Intent intent = new Intent(PLUGIN_PKG_PAYMENT);
        // 填充所携带的Bundle
        Bundle mBundle = new Bundle();
        // XML格式根据协议定义
        // 下面为示例代码，准确代码，请参考接口协议定义
        String orderAmount = ((Double) (amount * 100)).intValue() + "";
        String xml = XmlBuilder.getInstance().setMerchantOrderTime(DateUtils.formatOrderDateTime(new Date())).setMerchantOrderAmt(orderAmount).buildDefaultXml();
        mBundle.putString("xml", xml);
// 如果程序需要回调，请设置pkg
        mBundle.putString("pkg", MY_PKG);
// 将bundle置于intent中
        // 将Bundle传递给intent
        intent.putExtras(mBundle);
        // 使用intent转到手机POS
        this.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isFirst) {
            String result = UPOMP.getPayResult();
            if (result != null) {//成功
                ToastUtils.showMessage(this, result);
                showPaymentSuccessPage();
            } else {
                ToastUtils.showMessage(this, "交易失败");
            }

        }
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
    }


    @Override
    protected void onStart() {
        super.onStart();
        if (UserHolder.phoneNumber.equals("")) {
            this.finish();
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
